package org.csu.code.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.*;
import org.csu.code.api.persistence.FarmCommentMapper;
import org.csu.code.api.persistence.ItemCommentMapper;
import org.csu.code.api.persistence.LikeFarmCommentMapper;
import org.csu.code.api.persistence.LikeItemCommentMapper;
import org.csu.code.api.service.AccountService;
import org.csu.code.api.service.CommentService;
import org.csu.code.api.service.RedisService;
import org.csu.code.api.util.SensitiveFilterUtil;
import org.csu.code.api.vo.AccountVO;
import org.csu.code.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private LikeFarmCommentMapper likeFarmCommentMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private LikeItemCommentMapper likeItemCommentMapper;
    @Autowired
    private ItemCommentMapper itemCommentMapper;
    @Autowired
    private FarmCommentMapper farmCommentMapper;
    @Autowired
    private AccountService accountService;

    @Override
    public CommonResponse<FarmComment> selectFarmComment(int commentId) {
        FarmComment farmComment = farmCommentMapper.selectById(commentId);
        if (farmComment==null)
        {
            return CommonResponse.createForError("该评论不存在");
        }
        return CommonResponse.createForSuccess("查询成功",farmComment);
    }

    @Override
    public CommonResponse<ItemComment> selectItemComment(int commentId) {
        ItemComment itemComment = itemCommentMapper.selectById(commentId);
        if (itemComment==null)
        {
            return CommonResponse.createForError("该评论不存在");
        }
        return CommonResponse.createForSuccess("查询成功",itemComment);
    }

    @Override
    public CommonResponse<String> likeFarmComment(int commentId,HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        QueryWrapper<LikeFarmComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<LikeFarmComment> likeFarmCommentList = likeFarmCommentMapper.selectList(queryWrapper);
        for(LikeFarmComment likeFarmComment : likeFarmCommentList){
            int comment = likeFarmComment.getCommentId();
            if (comment == commentId)
            {
                return CommonResponse.createForError("已点赞过该条评论");
            }
        }
        LikeFarmComment likeFarmComment = new LikeFarmComment();
        likeFarmComment.setCommentId(commentId);
        likeFarmComment.setUserId(userId);
        int rows = likeFarmCommentMapper.insert(likeFarmComment);
        if (rows == 0){
            return CommonResponse.createForError("点赞失败");
        }
        FarmComment farmComment = farmCommentMapper.selectById(commentId);
        int likednum = farmComment.getLikednum();
        likednum++;
        farmComment.setLikednum(likednum);
        farmCommentMapper.updateById(farmComment);
        return CommonResponse.createForSuccess("点赞成功");
    }

    @Override
    public CommonResponse<String> likeItemComment(int commentId, HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        QueryWrapper<LikeItemComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<LikeItemComment> likeItemCommentList = likeItemCommentMapper.selectList(queryWrapper);
        for(LikeItemComment likeItemComment : likeItemCommentList){
            int comment = likeItemComment.getCommentId();
            if (comment == commentId)
            {
                return CommonResponse.createForError("已点赞过该条评论");
            }
        }
        LikeItemComment likeItemComment = new LikeItemComment();
        likeItemComment.setCommentId(commentId);
        likeItemComment.setUserId(userId);
        int rows = likeItemCommentMapper.insert(likeItemComment);
        if (rows == 0){
            return CommonResponse.createForError("点赞失败");
        }
        ItemComment itemComment = itemCommentMapper.selectById(commentId);
        int likednum = itemComment.getLikednum();
        likednum++;
        itemComment.setLikednum(likednum);
        itemCommentMapper.updateById(itemComment);
        return CommonResponse.createForSuccess("点赞成功");
    }

    @Override
    public CommonResponse<List<LikeFarmComment>> selectlikefarmComments(HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        QueryWrapper<LikeFarmComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<LikeFarmComment> likeFarmCommentList = likeFarmCommentMapper.selectList(queryWrapper);
        if(likeFarmCommentList.isEmpty()){
            return CommonResponse.createForError("无数据");
        }
        return CommonResponse.createForSuccess("查询成功",likeFarmCommentList);
    }

    @Override
    public CommonResponse<List<LikeItemComment>> selectlikeItemComments(HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        QueryWrapper<LikeItemComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<LikeItemComment> likeItemCommentList = likeItemCommentMapper.selectList(queryWrapper);
        if(likeItemCommentList.isEmpty()){
            return CommonResponse.createForError("无数据");
        }
        return CommonResponse.createForSuccess("查询成功",likeItemCommentList);
    }

    @Override
    public CommonResponse<String> addFarmComments(FarmComment farmComment,HttpServletRequest request) {
        String content = farmComment.getContent();
        int count = SensitiveFilterUtil.checkTxt(content);
        if (count!=0){
            return CommonResponse.createForError("评论内容不合规");
        }
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        Date date = new Date(System.currentTimeMillis());
        farmComment.setDate(date);
        farmComment.setUserId(userId);
        farmComment.setLikednum(0);
        int commentId = accountService.getNextId("farmcomnum");
        farmComment.setCommentId(commentId);
        int rows = farmCommentMapper.insert(farmComment);
        if(rows==0){
            return CommonResponse.createForError("评论农场失败");
        }
        return CommonResponse.createForSuccess("评论农场成功");
    }

    @Override
    public CommonResponse<String> addItemComments(ItemComment itemComment,HttpServletRequest request) {
        String content = itemComment.getContent();
        int count = SensitiveFilterUtil.checkTxt(content);
        if (count!=0){
            return CommonResponse.createForError("评论内容不合规");
        }
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        Date date = new Date(System.currentTimeMillis());
        itemComment.setDate(date);
        itemComment.setUserId(userId);
        itemComment.setLikednum(0);
        int commentId = accountService.getNextId("itemcomnum");
        itemComment.setCommentId(commentId);
        int rows = itemCommentMapper.insert(itemComment);
        if(rows==0){
            return CommonResponse.createForError("评论农产品失败");
        }
        return CommonResponse.createForSuccess("评论农产品成功");
    }

    @Override
    public CommonResponse<String> updateFarmComments(FarmComment farmComment,HttpServletRequest request) {
        String content = farmComment.getContent();
        int count = SensitiveFilterUtil.checkTxt(content);
        if (count!=0){
            return CommonResponse.createForError("评论内容不合规");
        }
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        Date date = new Date(System.currentTimeMillis());
        int commentId = farmComment.getCommentId();
        FarmComment farmComment1 = farmCommentMapper.selectById(commentId);
        if (farmComment1==null){
            return CommonResponse.createForError("此评论不存在");
        }else if (farmComment1.getUserId()!=userId){
            return CommonResponse.createForError("不能修改他人的评论");
        }else{
            farmComment1.setDate(date);
            farmComment1.setContent(content);
            int rows = farmCommentMapper.updateById(farmComment1);
            if (rows == 0)
            {
                return CommonResponse.createForError("修改农场评论失败");
            }
            return CommonResponse.createForSuccess("修改农场评论成功");
        }
    }

    @Override
    public CommonResponse<String> updateItemComments(ItemComment itemComment,HttpServletRequest request) {
        String content = itemComment.getContent();
        int count = SensitiveFilterUtil.checkTxt(content);
        if (count!=0){
            return CommonResponse.createForError("评论内容不合规");
        }
        String token = request.getHeader("token");
        int userId = Integer.parseInt((String) redisService.get(token));
        Date date = new Date(System.currentTimeMillis());
        int commentId = itemComment.getCommentId();
        ItemComment itemComment1 = itemCommentMapper.selectById(commentId);
        if (itemComment1==null){
            return CommonResponse.createForError("此评论不存在");
        }else if (itemComment1.getUserId()!=userId){
            return CommonResponse.createForError("不能修改他人的评论");
        }else{
            itemComment1.setDate(date);
            itemComment1.setContent(content);
            int rows = itemCommentMapper.updateById(itemComment1);
            if (rows == 0)
            {
                return CommonResponse.createForError("修改农产品评论失败");
            }
            return CommonResponse.createForSuccess("修改农产品评论成功");
        }
    }

    @Override
    public CommonResponse<List<FarmComment>> selectFarmCommentsByFarmId(int farmId) {
        QueryWrapper<FarmComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("farmid",farmId);
        List<FarmComment> farmCommentList = farmCommentMapper.selectList(queryWrapper);
        if(farmCommentList.isEmpty()){
            return CommonResponse.createForError("无数据");
        }
        return CommonResponse.createForSuccess("查询成功",farmCommentList);
    }

    @Override
    public CommonResponse<List<ItemComment>> selectItemCommentsByFarmId(int itemId) {
        QueryWrapper<ItemComment> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);
        List<ItemComment> itemCommentList = itemCommentMapper.selectList(queryWrapper);
        if(itemCommentList.isEmpty()){
            return CommonResponse.createForError("无数据");
        }
        return CommonResponse.createForSuccess("查询成功",itemCommentList);
    }
}
